package edu.gatech.cs2340.group7.wheresmystuffapp.views;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import edu.gatech.cs2340.group7.wheresmystuffapp.R;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Admin;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.CustomDate;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DataManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DatabaseHelper;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.FoundItem;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.ItemLocation;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.LostItem;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.User;
import edu.gatech.cs2340.group7.wheresmystuffapp.presenters.LoginPresenter;

public class LoginView extends Activity implements View.OnClickListener {
	private LoginPresenter myPresenter;
	private EditText etAskEmail;
	private EditText etAskPassword;
	private Button bLogin;
	private Button bSignUp;
	private Button bForgotAccount;
	private TextView tvFailedEntry;
	private User user = new User("user", "user", "2340");

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.login_button:
			System.out.println("Login Button");
			String email = etAskEmail.getText().toString();
			String password = etAskPassword.getText().toString();
			// myPresenter.
			myPresenter.login(email, password);
			break;
		case R.id.signup_button:

			Intent intent = new Intent(LoginView.this, CreateAccountView.class);
			intent.setPackage(getApplicationContext().getPackageName());

			startActivity(intent);

			break;
		case R.id.forgot_account:
			Intent intent2 = new Intent(this, ForgotAccountView.class);
			intent2.setPackage(getApplicationContext().getPackageName());
			startActivity(intent2);
			break;
		}

	}

	public String getPackageName() {
		return getApplicationContext().getPackageName();
	}

	public void setFailedEntryText(String result) {
		tvFailedEntry.setText(result);
		tvFailedEntry.setTextColor(Color.RED);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		myPresenter = new LoginPresenter(this);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
//
//		DataManager.addAccount("user", user);
//		DataManager.addAccount("Nick", new Admin("Nick", "Nick", "2340"));
//		DataManager.addAccount("Thomas", new Admin("Thomas", "Thomas", "2340"));
//		DataManager.addAccount("Sarah", new Admin("Sarah", "Sarah", "2340"));
//		DataManager.addAccount("Ai", new Admin("Ai", "Ai", "2340"));
//		DataManager.addAccount("Karim", new Admin("Karim", "Karim", "2340"));
		linkSetup();
	}

	/**
	 * Initializes all the views in the XML layout file, and sets the listeners for the buttons
	 */
	private void linkSetup() {
		etAskEmail = (EditText) findViewById(R.id.et_ask_email);
		etAskPassword = (EditText) findViewById(R.id.et_ask_password);
		bLogin = (Button) findViewById(R.id.login_button);
		bSignUp = (Button) findViewById(R.id.signup_button);
		bForgotAccount = (Button) findViewById(R.id.forgot_account);
		tvFailedEntry = (TextView) findViewById(R.id.failed_entry);

		bLogin.setOnClickListener(this);
		bSignUp.setOnClickListener(this);
		bForgotAccount.setOnClickListener(this);
	}

	/**
	 * Adds items to the database
	 */
	@SuppressWarnings("unused")
	private void addItems() {
		DatabaseHelper helper = new DatabaseHelper(this);
		ItemLocation il = new ItemLocation(0,0);
		
		LostItem m1 = new LostItem(user.getEmail(), "Misc", "m1", "asdf",
				il, 0, new CustomDate("02-05-2013"), null);
		LostItem m2 = new LostItem(user.getEmail(), "Misc", "m2", "asdf",
				il, 0, new CustomDate("06-01-2011"), null);
		LostItem h1 = new LostItem(user.getEmail(), "Heirloom", "h1", "asdf",
				il, 0, new CustomDate("11-11-2011"), null);
		LostItem h2 = new LostItem(user.getEmail(), "Heirloom", "h2", "asdf",
				il, 0, new CustomDate("12-09-2011"), null);
		LostItem k1 = new LostItem(user.getEmail(), "Keepsake", "k1", "asdf",
				il, 0, new CustomDate("01-19-2012"), null);
		LostItem k2 = new LostItem(user.getEmail(), "Keepsake", "k2", "asdf",
				il, 0, new CustomDate("02-29-2012"), null);

		FoundItem m3 = new FoundItem(user.getEmail(), "m3", "Misc", "asdf",
				il, new CustomDate("06-10-2012"), null);
		FoundItem m4 = new FoundItem(user.getEmail(), "m4", "Misc", "asdf",
				il, new CustomDate("07-01-2012"), null);
		FoundItem h3 = new FoundItem(user.getEmail(), "h3", "Heirloom", "asdf",
				il, new CustomDate("07-04-2012"), null);
		FoundItem h4 = new FoundItem(user.getEmail(), "h4", "Heirloom", "asdf",
				il, new CustomDate("11-01-2012"), null);
		FoundItem k3 = new FoundItem(user.getEmail(), "k3", "Keepsake", "asdf",
				il, new CustomDate("12-10-2012"), null);
		FoundItem k4 = new FoundItem(user.getEmail(), "k4", "Keepsake", "asdf",
				il, new CustomDate("03-05-2012"), null);

		
	}

	
}
